package com.aaa.member.controller;

import com.aaa.common.constant.ExceptionConstant;
import com.aaa.common.constant.ReturnCode;
import com.aaa.common.util.CustomException;
import com.aaa.common.util.Result;
import com.aaa.member.entity.TbMember;
import com.aaa.member.service.TbMemberService;
import io.swagger.models.auth.In;
import org.apache.ibatis.annotations.Param;
import org.apache.shiro.crypto.hash.Sha512Hash;
import org.apache.shiro.session.Session;
import org.jacoco.agent.rt.internal_43f5073.core.data.ISessionInfoVisitor;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * (TbMember)表控制层
 *
 * @author makejava
 * @since 2020-11-20 14:35:40
 */
@RestController
@RequestMapping("tbMember")
public class TbMemberController {
    /**
     * 服务对象
     */
    @Resource
    private TbMemberService tbMemberService;

    /**
     * 前台会员查询所有订单
     * @param session
     * @param orderstate
     * @return
     */
    @GetMapping("queryAllOrder")
    public Result queryAllOrder(Integer orderstate,HttpSession session){
        TbMember tbMember= (TbMember) session.getAttribute("userInfo");
        Integer memberId=tbMember.getId();
        //当ID空为时抛出自定义异常
        if (memberId==null) {
            throw new CustomException(ExceptionConstant.ID_NOT_NULL.getErrorCode(),
                    ExceptionConstant.ID_NOT_NULL.getErrorMessage());
        }
        return new Result(ReturnCode.SUCCESSS.getCode(),
                ReturnCode.SUCCESSS.getMsg(),tbMemberService.queryAllOrder(memberId,orderstate));
    }
    /**
     * 后台分页+查询客户优惠券信息
     * @param
     * @param
     * @return
     */
    @GetMapping("queryMemberCoupon")
    public Result queryMemberCoupon( Integer pageNo,
                                        Integer pageSize,
                                       String username,
                                      Integer usestatus
                                       ){
        if(pageNo==null||pageSize==null){
            throw new CustomException(ExceptionConstant.INVALID_ARGUMENT.getErrorCode(),
                    ExceptionConstant.INVALID_ARGUMENT.getErrorMessage());
        }
        return tbMemberService.queryMemberCoupon(pageNo,pageSize,username,usestatus);
    }

    /**
     * 前台根据会员id查询会员个人信息
     * @param
     * @param
     * @return
     */
    @GetMapping("queryMember")
    public Result queryMember(HttpSession session){
        TbMember tbMember= (TbMember) session.getAttribute("userInfo");
        return new Result(ReturnCode.SUCCESSS.getCode(),
                ReturnCode.SUCCESSS.getMsg(),
                this.tbMemberService.queryById(tbMember.getId()));
    }
    /**
     * 后台分页+查询所有会员信息
     * @param
     * @param
     * @return
     */
    @GetMapping("queryAllMember")
    public Result queryAllMember( Integer pageNo, Integer pageSize,TbMember tbMember){
        if(pageNo==null||pageSize==null){
            throw new CustomException(ExceptionConstant.INVALID_ARGUMENT.getErrorCode(),
                    ExceptionConstant.INVALID_ARGUMENT.getErrorMessage());
        }
        return tbMemberService.queryAllMember(pageNo,pageSize,tbMember);
    }

    /**
     * 添加注册用户
     * @param member
     * @return
     */
    @PostMapping("insert")
    public Result insert(@RequestBody TbMember member){
        return new Result(ReturnCode.SUCCESSS.getCode(),
                ReturnCode.SUCCESSS.getMsg(),
                this.tbMemberService.insert(member));
    }
    /**
     * 用户登录
     * @param tbMember
     * @return
     */
    @PostMapping("memberLogin")
    public Result memberlogin(@RequestBody TbMember tbMember) {
        if (tbMember.getUsername()==null&&tbMember.getPassword()==null){
            throw new CustomException(ExceptionConstant.INVALID_ARGUMENT.getErrorCode(),
                    ExceptionConstant.INVALID_ARGUMENT.getErrorMessage());
        }
        return  tbMemberService.memberlogin(tbMember.getUsername(),tbMember.getPassword());
    }

    @PutMapping("updatePassword")
    public Result updatePassword(@RequestBody TbMember tbMember,HttpSession session){
        TbMember oldTbMember= (TbMember) session.getAttribute("userInfo");
        //当ID空为时抛出自定义异常
        if (null ==oldTbMember.getId()) {
            throw new CustomException(ExceptionConstant.ID_NOT_NULL.getErrorCode(),
                    ExceptionConstant.ID_NOT_NULL.getErrorMessage());
        }
        tbMember.setId(oldTbMember.getId());
        return new Result(ReturnCode.SUCCESSS.getCode(),
                ReturnCode.SUCCESSS.getMsg(),tbMemberService.updatePassword(tbMember));
    }


    /**
     * 会员退出登录
     * @param session
     * @return
     */
    @GetMapping("exitLogin")
    public Result exitLogin(HttpSession session){
        //移除登录后的用户对象
        session.removeAttribute("userInfo");
        //销毁session
        session.invalidate();
        return new Result(ReturnCode.SUCCESSS.getCode(),
                ReturnCode.SUCCESSS.getMsg());
    }

}